import com.dinmax.flash.draw.common.Point
import com.dinmax.flash.draw.control.*
import com.dinmax.flash.draw.control.scroll.*
import com.dinmax.flash.draw.control.button.*

class ScrollControl
extends Scroll
implements SelectionEventExtended {
	
	private var base:MovieClip;
	private var barra:MovieClip;
	private var scrollView:ScrollView;
	private var UP_BUTTON:String="up";
	private var DOWN_BUTTON:String="down";
	private var MIDDLE_BUTTON:String="middle";
	private var buttonUp:PressButton;
	private var buttonDown:PressButton;
	private var buttonMiddle:PressButton;
	private var _min:Number;
	private var _max:Number;
	private var _step:Number;
	private var graphic_step:Number;
	private var _position:Number;
	private var _positions:Array; // con los ejes Y
	private var value:Number;
	
	function ScrollControl(container:MovieClip, name:String, view:ScrollView) {
		super(container, name);
		if (view==undefined) {
			scrollView=new ScrollViewClassic();
		} else {
			scrollView=view;
		}
		_position=0;
	}
	
	function getView():ScrollView {
		return scrollView;
	}

	public function getMin():Number{
		return this._min;
	}
	public function setMin(min:Number){
		this._min=min;
	}
	public function getMax():Number{
		return this._max;
	}
	public function setMax(max:Number){
		this._max=max;
	}
	
	public function setStep(step:Number){
		this._step=step;
	}

	public function getValue():Number{
		return this.value;
	}
	public function setValue(value:Number){
		this.value=value;
		change();
	}

	public function enable(){
		this.getClip()._visible=true;
	}
	public function disable(){
		this.getClip()._visible=false;
	}

	function select() {
		super.select();
	}
	
	function change() {
		super.change();
	}

	function draw(){
		reDraw();
	}

	function reDraw(){
		scrollView.reset();
		//Creo el movieBase
		removeMovieClip(base);
		base=createMovieClip();
		base._x=getLocation().getX();
		base._y=getLocation().getY();
		
		//tengo que setearle propiedades porque el flash es puto
		base.width=getSize().getX();
		base.height=getSize().getY();
		
		//dibujo el borde y los botones
		//despues de dibujarlos escucho sus eventos porque soy un SelectionEventExtended
		barra=scrollView.border(base); //listView.border(//el movie Base);
		buttonUp=scrollView.buttonUp(base);
		buttonUp.addSelectionEvent(this);
		buttonUp.setName(UP_BUTTON);

		buttonDown=scrollView.buttonDown(base);
		buttonDown.addSelectionEvent(this);
		buttonDown.setName(DOWN_BUTTON);

		buttonMiddle=scrollView.buttonMiddle(base);
		buttonMiddle.addSelectionEvent(this);
		buttonMiddle.setName(MIDDLE_BUTTON);
		if(PressButtonControl(buttonUp).getView() instanceof PressButtonViewAdvanced){
			var view:PressButtonViewAdvanced = PressButtonViewAdvanced(PressButtonControl(buttonUp).getView());
			PressButtonControlDraggable(buttonMiddle).setMin(new Point(view.getNormalFormat().getBorderSize(),buttonUp.getSize().getY()));
			PressButtonControlDraggable(buttonMiddle).setMax(new Point (view.getNormalFormat().getBorderSize(), getSize().getY() - buttonUp.getSize().getY() - buttonMiddle.getSize().getY()));
		}
		
		calculatePositions();
	}
	
	public function up(){
		if(_position > 0){
			_position--;
			buttonMiddle.setLocation(new Point(buttonMiddle.getLocation().getX(), _positions[_position]));
		}
		this.setValue(_min-(_position*_step));
	}
	public function down(){
		if(_position < _positions.length-1){
			_position++;
			buttonMiddle.setLocation(new Point(buttonMiddle.getLocation().getX(), _positions[_position]));
		}
		this.setValue(_min-(_position*_step));
	}
	private function dropButton(){
		var nPos:Number=(buttonMiddle.getLocation().getY());
		for(var i:Number=0;i< _positions.length;i++){
			if(_positions[i] > nPos && nPos - _positions[0] > _step/2){
				buttonMiddle.setLocation(new Point(buttonMiddle.getLocation().getX(), _positions[i]));
				_position=i;
			break;
			} else {
				buttonMiddle.setLocation(new Point(buttonMiddle.getLocation().getX(), _positions[0]));
				_position=0;
			}
		}
		setValue(_min-(_position*_step));
	}
	
	private function calculatePositions(){
		var nCuenta:Number= Math.ceil((Math.abs(_max-_min))/_step);
		_positions=new Array(nCuenta+1);
		graphic_step=(getSize().getY()- buttonUp.getSize().getY()*2 - buttonMiddle.getSize().getY()+5)/(nCuenta);
		for(var i:Number=0;i< _positions.length ;i++){
			_positions[i]=buttonMiddle.getLocation().getY()+ graphic_step * i;
		}
		
	}

	public function selectedExtended(source:Control){
		switch(source.name){
			case UP_BUTTON:
				this.up();			
			break;
			case DOWN_BUTTON:
				this.down();
			break;
			case MIDDLE_BUTTON:
				this.dropButton();
			break;
		}
	}

	//queverga ESTO ES POR IMPLEMENTAR LA INTERFAZ
	public function selected(){
	}
			
		
} // FIN